using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace Microsoft.Phone.Controls
{
	public class Transition : ITransition
	{
		private CacheMode _cacheMode;
		private UIElement _element;
		private bool _isHitTestVisible;
		private Storyboard _storyboard;
		public event EventHandler Completed
		{
			add
			{
				this._storyboard.add_Completed(value);
			}
			remove
			{
				this._storyboard.remove_Completed(value);
			}
		}
		public Transition(UIElement element, Storyboard storyboard)
		{
			if (element == null)
			{
				throw new ArgumentNullException("element");
			}
			if (storyboard == null)
			{
				throw new ArgumentNullException("storyboard");
			}
			this._element = element;
			this._storyboard = storyboard;
		}
		public void Begin()
		{
			this.Save();
			this._storyboard.add_Completed(new EventHandler(this.OnCompletedRestore));
			this._storyboard.Begin();
		}
		private void OnCompletedRestore(object sender, EventArgs e)
		{
			this.Restore();
		}
		public ClockState GetCurrentState()
		{
			return this._storyboard.GetCurrentState();
		}
		public TimeSpan GetCurrentTime()
		{
			return this._storyboard.GetCurrentTime();
		}
		public void Pause()
		{
			this._storyboard.Pause();
		}
		private void Restore()
		{
			if (!(this._cacheMode is BitmapCache))
			{
				this._element.set_CacheMode(this._cacheMode);
			}
			if (this._isHitTestVisible)
			{
				this._element.set_IsHitTestVisible(this._isHitTestVisible);
				return;
			}
			this._element.set_IsHitTestVisible(true);
		}
		public void Resume()
		{
			this._storyboard.Resume();
		}
		private void Save()
		{
			this._cacheMode = this._element.get_CacheMode();
			if (!(this._cacheMode is BitmapCache))
			{
				this._element.set_CacheMode(TransitionFrame.BitmapCacheMode);
			}
			this._isHitTestVisible = this._element.get_IsHitTestVisible();
			if (this._isHitTestVisible)
			{
				this._element.set_IsHitTestVisible(false);
			}
		}
		public void Seek(TimeSpan offset)
		{
			this._storyboard.Seek(offset);
		}
		public void SeekAlignedToLastTick(TimeSpan offset)
		{
			this._storyboard.SeekAlignedToLastTick(offset);
		}
		public void SkipToFill()
		{
			this._storyboard.SkipToFill();
		}
		public void Stop()
		{
			this._storyboard.Stop();
			this.Restore();
		}
	}
}
